package com.pond.pond.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.pond.pond.client.DeptClient;
import com.pond.pond.domain.vo.FishFlowRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pond.common.log.annotation.Log;
import com.pond.common.log.enums.BusinessType;
import com.pond.common.security.annotation.RequiresPermissions;
import com.pond.pond.domain.FishFlowRecord;
import com.pond.pond.service.IFishFlowRecordService;
import com.pond.common.core.web.controller.BaseController;
import com.pond.common.core.web.domain.AjaxResult;
import com.pond.common.core.utils.poi.ExcelUtil;
import com.pond.common.core.web.page.TableDataInfo;

/**
 * 鱼流转记录Controller
 * 
 * @author ruoyi
 * @date 2025-05-13
 */
@RestController
@RequestMapping("/fishrecord")
public class FishFlowRecordController extends BaseController
{
    @Autowired
    private IFishFlowRecordService fishFlowRecordService;
    @Autowired
    private DeptClient deptClient;

    /**
     * 查询鱼流转记录列表
     */
    @RequiresPermissions("pond:fishrecord:list")
    @GetMapping("/list")
    public TableDataInfo list(FishFlowRecordVo fishFlowRecord)
    {
        Long deptId = fishFlowRecord.getDeptId();

        if(deptId != null){
            deptClient.checkDeptDataScope((deptId));
            fishFlowRecord.setDeptId(deptId);
        }else{
            fishFlowRecord.setDeptId(null);
        }

        fishFlowRecord.setDeptId(deptId);

        startPage();
        List<FishFlowRecordVo> list = fishFlowRecordService.selectFishFlowRecordList(fishFlowRecord);
        return getDataTable(list);
    }

    /**
     * 导出鱼流转记录列表
     */
    @RequiresPermissions("pond:fishrecord:export")
    @Log(title = "鱼流转记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FishFlowRecord fishFlowRecord)
    {
        List<FishFlowRecord> list = fishFlowRecordService.selectFishFlowRecordList1(fishFlowRecord);
        ExcelUtil<FishFlowRecord> util = new ExcelUtil<FishFlowRecord>(FishFlowRecord.class);
        util.exportExcel(response, list, "鱼流转记录数据");
    }

    /**
     * 获取鱼流转记录详细信息
     */
    @RequiresPermissions("pond:fishrecord:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fishFlowRecordService.selectFishFlowRecordById(id));
    }

    /**
     * 新增鱼流转记录
     */
    @RequiresPermissions("pond:fishrecord:add")
    @Log(title = "鱼流转记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FishFlowRecord fishFlowRecord)
    {
        if(fishFlowRecord.getDeptId() != null){
            deptClient.checkDeptDataScope(fishFlowRecord.getDeptId());
        }
        return toAjax(fishFlowRecordService.insertFishFlowRecord(fishFlowRecord));
    }

    /**
     * 修改鱼流转记录
     */
    @RequiresPermissions("pond:fishrecord:edit")
    @Log(title = "鱼流转记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FishFlowRecord fishFlowRecord)
    {
        deptClient.checkDeptDataScope(fishFlowRecord.getDeptId());
        return toAjax(fishFlowRecordService.updateFishFlowRecord(fishFlowRecord));
    }

    /**
     * 删除鱼流转记录
     */
    @RequiresPermissions("pond:fishrecord:remove")
    @Log(title = "鱼流转记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids){
            FishFlowRecordVo fishFlowRecord = fishFlowRecordService.selectFishFlowRecordById(id);
            if (fishFlowRecord.getDeptId() != null){
                deptClient.checkDeptDataScope(fishFlowRecord.getDeptId());
            }
        }
        return toAjax(fishFlowRecordService.deleteFishFlowRecordByIds(ids));
    }
}
